package com.soujiayi.activity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.soujiayi.model.ProductAttributes;
import com.soujiayi.model.ProductSelectItem;
import com.soujiayi.util.Constants;

public class ProductParametersActivity extends Activity {

	private ProductAttributes attributes;
	private List<ProductSelectItem> parameters;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		((MyApplication) this.getApplication()).addActivity(this);
		setContentView(R.layout.product_parameters);

		Intent intent = this.getIntent();
		
		attributes = (ProductAttributes) intent.getSerializableExtra(Constants.PRODUCT_ATTRIBUTE);
		
		parameters = attributes.getAttrlist();

		TableLayout parametersTable = (TableLayout) findViewById(R.id.product_parameters_table);
		
		if (parameters != null) {
			for (int i = 0; i < parameters.size(); i ++) {
				ProductSelectItem item = parameters.get(i);
				TableRow tableRow = new TableRow(this);
				if(parameters.size() == 1){
					tableRow.setBackgroundResource(R.drawable.table_row_single);
				}else{
					if(i == 0){
						tableRow.setBackgroundResource(R.drawable.table_row_first);
					}else if(i == parameters.size() - 1){
						tableRow.setBackgroundResource(R.drawable.table_row_last);
					}else{
						tableRow.setBackgroundResource(R.drawable.table_row_normal);
					}
				}

				TextView tv1 = new TextView(this);
				//tv1.setGravity(Gravity.CENTER);
				tv1.setPadding(8, 8, 8, 8);
				tv1.setText(item.getName() + ":");
				tv1.setTextSize(15);
				tv1.setTextColor(Color.BLACK);
				tableRow.addView(tv1);

				TextView tv2 = new TextView(this);
				tv2.setPadding(8, 8, 8, 8);
				tv2.setText(item.getValue());
				tv2.setTextSize(15);
				tv2.setTextColor(Color.BLACK);
				tableRow.addView(tv2);

				parametersTable.addView(tableRow);
				i++;
			}
		}

	}

	public void backToHome(View view) {
		Intent intent = new Intent(ProductParametersActivity.this,
				SearchActivity.class);
		startActivity(intent);
	}

	public void back(View view) {
		this.finish();
		overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		//如果按下的是返回键，并且没有重复
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
			return false;
		}
		return false;
	}

}
